<?php


namespace app\api\model\Order;


use think\facade\Db;
use think\Model;
use think\model\relation\HasMany;

class OrderModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = 'order';

    /**
     * 关联定义
     * @return HasMany
     */
    public function list()
    {
        return $this->hasMany(OrderGoodsModel::class,'order_id','id');
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param array $where
     * @param string $field
     * @return array
     * @throws
     */
    public static function getOrderListWherePage($page,$limit,$where = [],$field = "*")
    {
        $start = ceil(($page-1)*$limit);
        $list = OrderModel::with('list')
                ->visible(['list'=>['id','order_id','goods_id','type','number','goods_info','goods_price','suk','unique','is_postage','postage','total_price','total_integral','deduction_price','agnet_uid1','agent_amount1','agent_status1','agnet_uid2','agent_amount2','agent_status2','agnet_uid3','agent_amount3','agent_status3','mer_id','lgoistics_com','lgoistics_num','lgoistics_name','status']])
                ->field($field)
                ->where($where)
                ->limit($start,$limit)
                ->order('add_time desc')
                ->select()->toArray();
        return $list;
    }

    /**
     * @param $page
     * @param $limit
     * @param array $where
     * @return array
     * @throws
     */
    public static function getOrderListWhereOrPage($page,$limit,$where = [])
    {
        $start = ceil(($page-1)*$limit);
        $list = Db::name('order')->whereOr($where)->limit($start,$limit)->select()->toArray();
        return $list;
    }

    /**
     * 统计
     * @param $where
     * @return int
     */
    public static function getOrderListWhereCount($where)
    {
        $count = Db::name('order')->where($where)->count("id");
        return $count;
    }

    /**
     * 统计
     * @param $where
     * @return int
     */
    public static function getOrderListWhereOrCount($where)
    {
        $count = Db::name('order')->whereOr($where)->count("id");
        return $count;
    }


    /**
     * 根据条件修改
     * @param $where
     * @param $save
     * @return int
     * @throws
     */
    public static function updateOrderWhere($where,$save)
    {
        $flag = Db::name('order')->where($where)->update($save);
        return $flag;
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @param string $field
     * @return array
     * @throws
     */
    public static function getOrderListWhereMore($where,$field="o.*")
    {
        $list = Db::name('order')
            ->alias('o')
            ->join('user u','u.id = o.uid','left')
            ->field($field)
            ->where($where)
            ->select()->toArray();
        return $list;
    }

    /**
     * 根据条件查询单条
     * @param $where
     * @return array|Model|null
     * @throws
     */
    public static function getOrderWhere($where)
    {
        $order = OrderModel::with('list')
            ->visible(['list'=>['id','order_id','goods_id']])
            ->where($where)
            ->find();
        return $order;
    }

    /**
     * 添加
     * @param $data
     * @return int|string
     */
    public static function addOrder($data)
    {
        $id = Db::name('order')->insertGetId($data);
        return $id;
    }

    /**
     * 关联查询
     * @param $where
     * @param string $field
     * @return array|Model|null
     * @throws
     */
    public static function getOrderWhereJoinMore($where,$field = "o.*")
    {
        $list = Db::name('order')
                    ->alias('o')
                    ->join('order_goods og','og.order_id = o.id','left')
                    ->where($where)
                    ->field($field)
                    ->select()->toArray();
        return $list;
    }

    /**
     * 关联删除
     * @param $where
     * @return bool
     * @throws
     */
    public static function deleteOrderWhere($where)
    {
        $order = OrderModel::with('list')->where($where)->find();
        $flag = $order->together(['list'])->delete();
        return $flag;
    }

}